<?php
@set_time_limit(0);
include_once("includes/global.php");
$b2bbuilder_auth=bgetcookie("USERID");
$buid=$b2bbuilder_auth['0'];
$action = isset($_POST['action']) ?  $_POST['action'] : '';
if($action == 'get_city'){
	$pid = $_POST['id'];
	$sql = "select * from ".DISTRICT." where pid = '$pid' ";
	$db->query($sql);
	$city_list = $db->getRows();
	$city = '<option value="">请选择</option>';
	foreach($city_list as $key => $val){
		$city .='<option value="'.$val['id'].'">'.$val['name'].'</option>';
	}
	echo json_encode(['content' => $city]);
	exit;
}
//删除票品
elseif($action == 'delete_product'){
	$id = $_POST['id'];
	$sql = "delete from ".SETMEAL." where id = '$id' ";
	if($db->query($sql)){
		echo json_encode(['status' => true]);
		exit;
	} else {
		echo json_encode(['status' => false,'msg' => '删除失败']);
		exit;
	}

}
//答疑
elseif($action == 'submit_quetsion' ){
	$param = $_POST;
	if(empty($param['buid'])){
		echo json_encode(['status' => false,'msg' => '请登录','url' => $config['weburl'].'/login.php']);
		return false;
	}
	$sql = "select * from ".PRO." where id = '{$param['pid']}' ";
	$db->query($sql);
	$product_info = $db->fetchRow();
	if(empty($product_info)){
		echo json_encode(['status' => false,'msg' => '票品不存在']);
		return false;
	}

	$usql = "select * from ".MEMBER." where userid = '{$param['buid']}' ";
	$db->query($usql);
	$member_info = $db->fetchRow();
	if(empty($member_info)){
		echo json_encode(['status' => false,'msg' => '用户不存在']);
		return false;
	}
	$time = time();
	$isql = "insert into ".QUESTION." (`catid`,`product_id`,`product_name`,`member_id`,`member_name`,`question`,`question_time`,`status`)
	values ('{$product_info['catid']}','{$product_info['id']}','{$product_info['pname']}','{$member_info['userid']}','{$member_info['user']}','{$param['content']}','$time','1')";
	if($db->query($isql)){
		echo json_encode(['status' => true,'url' => $config['weburl'].'/main.php?m=product&s=admin_question' ]);
		return false;
	} else {
		echo json_encode(['status' => false,'msg' => '提交失败']);
		return false;
	}

}
//送花
elseif($action == 'send_flower'){
	$id = $_POST['pid'];
	if(empty($buid)){
		echo json_encode(['status' => false, 'msg'=>'请登录']);
		return false;
	}
	$db->query("select * from ".FLOWER." where pid = '$id' and uid = '$buid' ");
	$is_flower = $db->fetchRow();
	if(!empty($is_flower)){
		echo json_encode(['status' => false, 'msg'=>'已送花']);
		return false;
	} else {
		$time = time();
		$sql = "insert into ".FLOWER." (`pid`,`uid`,`add_at`) values ('$id','$buid','$time')";
		if($db->query($sql)){
			echo json_encode(['status' => true]);
			return false;
		} else {
			echo json_encode(['status' => false, 'msg'=>'送花失败']);
			return false;
		}
	}
}

//取票地址
elseif($action == 'pick_city_info'){
	$id = $_POST['id'];
	$db->query("select * from ".SINCE." where id = '$id'");
	$city_info = $db->fetchRow();
	echo json_encode([ 'msg'=>$city_info['address'] ]);
	return false;
}
//检验身份证号码 check_card_no
elseif($action == 'check_card_no'){
	include_once("includes/idcard.class.php");
	$param = $_POST;
	$card_obj = new idcard($param['real_card_no']);
	$res = $card_obj->isIdCard();
	if($param['real_card_type'] == 1){
		if(!$res){
			echo json_encode([ 'status'=>false,'msg' => '证件号码无效，请填写正确的证件号码']);
		} else {
			echo json_encode([ 'status'=>true]);
		}
	} elseif($param['real_card_type'] == 2){
		echo json_encode([ 'status'=>true]);

	}



	return false;
}


?>